package com.atguigu.Mytest;

public class DeadLockDemo {
    static Object lockA = new Object();
    static Object lockB = new Object();

    public static void main(String[] args) {

        new Thread(()->{
            synchronized (lockA){
                System.out.println(Thread.currentThread().getName() + "/t"  + "已经持有A锁，现在想要持有B锁");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lockB){
                    System.out.println(Thread.currentThread().getName() + "/t"  + "获得B锁成功");
                }
            }
        },"A").start();

        new Thread(()->{
            synchronized (lockB){
                System.out.println(Thread.currentThread().getName() + "/t"  + "已经持有B锁，现在想要持有A锁");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lockA){
                    System.out.println(Thread.currentThread().getName() + "/t"  + "获得A锁成功");
                }
            }
        },"B").start();
    }

}
